import Vue from "vue";
import VueRouter from "vue-router";
import { Toast } from "vant";
import utils from "../tools/utils";
import store from "@/store/index";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "frame",
    redirect: "/goodslist"
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/index"),
    meta: {
      title: "登录",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/me",
    name: "me",
    component: () => import("../views/me/index"),
    meta: {
      title: "个人中心",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/goodslist",
    name: "goodslist",
    component: () => import("../views/goodslist/index"),
    meta: {
      title: "首页",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/megoodslist",
    name: "megoodslist",
    component: () => import("../views/megoodslist/index"),
    meta: {
      title: "我发布的商品",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/megoodslist2",
    name: "megoodslist2",
    component: () => import("../views/megoodslist/index"),
    meta: {
      title: "我租凭的商品",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/megoodslist3",
    name: "megoodslist3",
    component: () => import("../views/megoodslist/index"),
    meta: {
      title: "我喜欢的商品",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/megoodslist4",
    name: "megoodslist4",
    component: () => import("../views/megoodslist/index"),
    meta: {
      title: "我收藏的商品",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/notifications",
    name: "notifications",
    component: () => import("../views/notifications/index"),
    meta: {
      title: "订单中心",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/changepsd",
    name: "changepsd",
    component: () => import("../views/changepsd/index"),
    meta: {
      title: "修改密码",
      keepAlive: false,
      index: 0
    }
  },
  {
    path: "/goods",
    name: "goods",
    component: () => import("../views/goods/index"),
    meta: {
      title: "商品详情",
      keepAlive: false,
      index: 0
    }
  }
];

const router = new VueRouter({
  mode: "hash",
  routes,
  scrollBehavior: () => ({ y: 0 })
});

router.beforeEach((to, from, next) => {
  const uid = utils.getCookie("UID") || "";
  if (uid) {
    if (to.path === "/changepsd") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", false);
    }
    if (to.path === "/notifications") {
      store.dispatch("setVisible", false);
      store.dispatch("setMenuVisible", true);
    }
    if (to.path === "/megoodslist") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", true);
    }
    if (to.path === "/megoodslist2") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", true);
    }
    if (to.path === "/megoodslist3") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", true);
    }
    if (to.path === "/megoodslist4") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", true);
    }
    if (to.path === "/goods") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", false);
    }
    if (to.path === "/login") {
      store.dispatch("setVisible", true);
      store.dispatch("setMenuVisible", false);
    }
    if (to.path === "/me") {
      store.dispatch("setVisible", false);
      store.dispatch("setMenuVisible", true);
    }
    if (to.path === "/goodslist") {
      store.dispatch("setVisible", false);
      store.dispatch("setMenuVisible", true);
    }
    next();
  } else {
    if (to.path === "/login") {
      if (to.path === "/login") {
        store.dispatch("setVisible", true);
        store.dispatch("setMenuVisible", false);
      }
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
